package stm

import (
	"fmt"
	"github.com/gin-gonic/gin"
	nhttp "net/http"
	"wzgames/internal/admin/adc"
	"wzgames/internal/admin/adsv/stmsv"
	"wzgames/internal/admin/api/mid"
	"wzgames/internal/admin/api/wzabind"
	"wzgames/internal/admin/model"
	"wzgames/internal/response"
	"wzgames/pkg/server/http"
	"wzgames/pkg/server/http/types"
	"wzgames/pkg/utils/stringutil"
)

func RegisterRoute(pGroup *gin.RouterGroup) {
	bizGroup := pGroup.Group("statement")
	http.MethodMapper(bizGroup, types.GET)(unCheckListFn())
	http.MethodMapper(bizGroup, types.GET)(nodeRoundBetPendingList())
	http.MethodMapper(bizGroup, types.GET)(aviatorNodeRoundFn())
	http.MethodMapper(bizGroup, types.GET)(rouNodeRoundFn())
	http.MethodMapper(bizGroup, types.GET)(betPendingStepFn())

	http.MethodMapper(bizGroup, types.GET)(userBetsFn())
	http.MethodMapper(bizGroup, types.GET)(userBetCountFn())
	http.MethodMapper(bizGroup, types.GET)(userBetSumFn())
}

var unCheckListFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/uncheck")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		resp := model.Response{}
		var params wzabind.BetPendingSsReq
		if err := c.ShouldBind(&params); err != nil {
			//logger.Error("err == ", zap.String("error", err.Error()))
			resp.Code = response.ErrBadRequest
			c.JSON(nhttp.StatusOK, resp)
			return
		}
		if params.Page <= 0 {
			params.Page = 1
		}
		if params.PageSize <= 0 {
			params.PageSize = 50
		}

		ret, count, code := stmsv.SV().GetBetPendingUnCheck(adm, params)
		resp.Code = code
		if code == 0 {
			resp.Data = gin.H{"total": count, "data": ret}
		}

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}

var nodeRoundBetPendingList = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/node_round_pending")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		resp := model.Response{}
		var params wzabind.BetPendingSsReq
		if err := c.ShouldBind(&params); err != nil {
			//logger.Error("err == ", zap.String("error", err.Error()))
			resp.Code = response.ErrBadRequest
			c.JSON(nhttp.StatusOK, resp)
			return
		}
		if params.PageSize <= 0 {
			params.PageSize = 20
		}

		ret, code := stmsv.SV().GetGameBetPendingRounds(params.GameId, int64(params.PageSize))
		resp.Code = code
		if code == 0 {
			resp.Data = gin.H{"data": ret}
		}

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}

var aviatorNodeRoundFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/avt_node_round_info")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		resp := model.Response{}
		var params wzabind.BetPendingSsReq
		if err := c.ShouldBind(&params); err != nil {
			//logger.Error("err == ", zap.String("error", err.Error()))
			resp.Code = response.ErrBadRequest
			c.JSON(nhttp.StatusOK, resp)
			return
		}
		rd, code := stmsv.SV().GetAviatorRound(params.RoundId, params.InstId)
		if code != 0 {
			resp.Code = code
			c.JSON(nhttp.StatusOK, resp)
			return
		}
		nodeRd, c1 := stmsv.SV().GetAviatorNodeRound(params.NodeId, params.InstId, params.RoundId)
		if c1 == response.ErrInternal {
			resp.Code = code
			c.JSON(nhttp.StatusOK, resp)
			return
		}

		resp.Code = code
		if code == 0 {
			resp.Data = gin.H{"data": rd, "node": nodeRd}
		}

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}

var rouNodeRoundFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/rou_node_round_info")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		resp := model.Response{}
		var params wzabind.BetPendingSsReq
		if err := c.ShouldBind(&params); err != nil {
			//logger.Error("err == ", zap.String("error", err.Error()))
			resp.Code = response.ErrBadRequest
			c.JSON(nhttp.StatusOK, resp)
			return
		}
		rd, code := stmsv.SV().GetRouRound(params.RoundId, params.InstId)
		if code != 0 {
			resp.Code = code
			c.JSON(nhttp.StatusOK, resp)
			return
		}
		nodeRd, c1 := stmsv.SV().GetRouNodeRound(params.NodeId, params.InstId, params.RoundId)
		if c1 == response.ErrInternal {
			resp.Code = code
			c.JSON(nhttp.StatusOK, resp)
			return
		}

		resp.Code = code
		if code == 0 {
			resp.Data = gin.H{"data": rd, "node": nodeRd}
		}

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}

var betPendingStepFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/bet_step_info/:betId")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		betId, _ := stringutil.ToUint64(c.Param("betId"), 0)
		if betId <= 0 {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrBadRequest})
			return
		}
		resp := model.Response{}
		payoutEnt, stepEnt, code := stmsv.SV().GetBetPendingStepInfo(betId)
		resp.Code = code
		if code == 0 {
			resp.Data = gin.H{"payEnt": payoutEnt, "stepEnt": stepEnt}
		}

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}
